<template>
  <!-- 如果不加这个会报错，直接从null数组里面拿数据会报错，必须等goods存在才获取数据 -->
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <!-- 如果暂时没有这个面包屑页面但是配置了跳转路径会报错 -->
        <XtxBreadItem to="/">{{ goods.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <!-- 图片预览区 -->
          <XtxImageView :image-list="goods.mainPictures" />
          <GoodsSale :goods="goods" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodsInfo :goods="goods" />
          <!-- 规格选择组件 -->
          <XtxGoodSku :goods="goods" @change="goodSkuChangeFn" />
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs"></div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { reqFindGoods } from '@/api/category'
import { useRoute } from 'vue-router'
import { ref } from 'vue-demi'
import GoodsInfo from './components/goods-info.vue'
import GoodsSale from './components/goods-sale.vue'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsInfo,
    GoodsSale
  },
  setup () {
    const goods = ref(null)
    const route = useRoute()
    console.log(route.params.id)
    reqFindGoods(route.params.id).then(({ result }) => {
      console.log(result)
      goods.value = result
    })
    // 选择规格组件的使用
    const goodSkuChangeFn = (val) => {
      // console.log('商品详情', goods.value)
      // console.log('规格选择组件子传父结果', val)
      // 如果选择了规格，才更新商品信息，没全部选中的话会有一个空数组
      if (val.skuId) {
        // goods.value.price = val.price
        // goods.value.oldPrice = val.oldPrice
        // goods.value.inventory = val.inventory
        // goods.value.skuId = val.skuId
        // goods.value.specsText = val.specsText
        // 🔔 通过遍历对象化简代码
        for (const key in val) {
          goods.value[key] = val[key]
        }
      }
    }
    return { goods, goodSkuChangeFn }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
